<template>
  <div class="modal">
    <div class="mask" @click="changeIsShowLoginModel(false)"></div>
    <div class="login">
      <div class="close" @click="changeIsShowLoginModel(false)"></div>
      <section class="nav">
        <a href="#" :class="{ active: phoneShow }" @click="phoneShow = true"
          >手机登录</a
        >
        |
        <a href="#" :class="{ active: !phoneShow }" @click="phoneShow = false"
          >微信扫码登录</a
        >
      </section>
      <!-- 内容区域 -->
      <div class="content">
        <!-- 手机号登录内容 -->
        <div class="phoneLogin" v-show="phoneShow">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              class="ipt"
              v-model="phoneNum"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="array"
            ></slide-verify>
          </div>
          <div class="row">
            <input type="text" placeholder="请输入短信验证码" class="ipt" />
            <button class="btncode" @click="getCode" v-show="!isShowCount">
              获取验证码
            </button>
            <button v-show="isShowCount">{{ count + "s" }}</button>
          </div>
          <div class="row">
            <button class="loginbtn" @click="submitFn">登录</button>
          </div>
        </div>
        <!-- 微信扫码登录内容 -->
        <div class="qrLogin" v-show="!phoneShow">微信扫码登录二维码</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import { SendSMSAPI } from "@/request/Api";
export default {
  data() {
    return {
      phoneShow: true,
      msg: "",
      array: [require("../assets/img/02e4b0cfea853ef2d6fd753031ce7514.png")],
      phoneNum: "",
      count: 5,
      isShowCount: false,
    };
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModel: "showModel/changeIsShowLoginModel",
    }),
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    // 获取验证码
    async getCode() {
      // 1.
      let reg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (!reg.test(this.phoneNum)) {
        alert("请输入正确的手机号码");
        return;
      }
      // 2.滑块验证码不正确 提示
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        alert("请滑动滑块验证码！");
        return; // 打断，不执行登录成功
      }
      // 3.获取验证码 -- 60s倒计时功能
      // 获取验证码 -- 发请求
      let res = await SendSMSAPI({
        phone: this.phoneNum,
      });
      console.log("发送短信验证码res:", res);
      if (res.code == 0) {
        alert("发送成功");
      }
      // 倒计时 循环定时器递减数字count
      this.isShowCount = true;
      let timer = setInterval(() => {
        this.count--;
        if (this.count <= 0) {
          this.count = 3;
          // 停止 clear
          clearInterval(timer);
          // 取反切换按钮
          this.isShowCount = false;
        }
      }, 1000);
    },
    // 登录
    submitFn() {
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        alert("请滑动滑块验证码！");
        return; // 打断，不执行登录成功
      }
      alert("登陆成功！");
    },
  },
};
</script>

<style lang="less" scoped>
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .mask {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .login {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background: url("../assets/img/login-box-bg.png");
    .close {
      position: absolute;
      width: 22px;
      height: 22px;
      right: 55px;
      top: 10px;
      background: url("../assets/img/close.png");
    }
    .nav {
      padding-top: 50px;
      text-align: center;
      a {
        text-decoration: none;
        color: gray;
        &.active {
          color: #000;
        }
      }
    }
    .content {
      width: 355px;
      height: 300px;
      margin: 10px auto 0;

      // 登录样式
      .phoneLogin {
        .row {
          display: flex;
          margin-bottom: 20px;
          .ipt {
            height: 50px;
            outline: none;
            flex: 1;
            text-indent: 1em;
          }
          button {
            width: 85px;
          }
          .btncode {
            background-color: #0a328e;
            color: #fff;
            margin-left: 10px;
          }
          .loginbtn {
            background-color: #0a328e;
            color: #fff;
            height: 50px;
            width: 100%;
          }
        }
      }
    }
  }
}

// 滑块验证
//  /deep/ 深度的意思
/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>